﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//using Microsoft.Practices.EnterpriseLibrary.Data;
using ChinaDynastyTours.Controls;
using ChinaDynastyTours.Helpers;
using System.Threading;
using IToursDataService;
using DevExpress.XtraGrid.Views.Grid;
//using ChinaDynastyTours.Assets;

namespace ChinaDynastyTours.Views
{

    public partial class SeriesTeamGroupManagerForm : DevExpress.XtraEditors.XtraForm
    {
        //private bool m_checkStatus = false;
        private DataTable _dtTeamgroupListData;

        public SeriesTeamGroupManagerForm()
        {
            InitializeComponent();
        }

        private void btnLoadTeamgroupData_Click(object sender, EventArgs e)
        {
            DevExpress.Utils.ToolTipController tip = ApplicationHelper.ShowLoadTip("正在加载数据...", singleSelectTeamGridControl);
            singleSelectTeamGridControl.DataSource = WcfClient.UseService<ITeamGroupCtrl, DataTable>(t => t.GetTeamGroupDataForRestaurantAndBillBatchCopy(ApplicationHelper.CurrentLoginProviderNameAndID[1]));
            tip.HideHint();

            DevExpress.Utils.ToolTipController tip1 = ApplicationHelper.ShowLoadTip("正在加载数据...", mutilSelectTeamGridControl);
            _dtTeamgroupListData = WcfClient.UseService<ITeamGroupCtrl, DataTable>(t => t.GetTeamGroupDataForRestaurantAndBillBatchCopy(ApplicationHelper.CurrentLoginProviderNameAndID[1]));
            mutilSelectTeamGridControl.DataSource = AddGridSelectedColumn(_dtTeamgroupListData);
            tip1.HideHint();

        }
        private void btnBillBatchCopy_Click(object sender, EventArgs e)
        {
            string tip = string.Format("即将复制团‘{0}’中账单数据到目标团中，确定吗？", singleSelectTeamGridView.GetRowCellValue(singleSelectTeamGridView.FocusedRowHandle, "TourgroupName"));
            if (MessageBox.Show(tip, "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                try
                {
                    Cursor.Current = Cursors.WaitCursor;
                    btnBillBatchCopy.Enabled = false;
                    string sourceTourgroupId = singleSelectTeamGridView.GetRowCellValue(singleSelectTeamGridView.FocusedRowHandle, "TourgroupID").ToString();
                    int[] rowIds = MutilRow(mutilSelectTeamGridView);
                    if (rowIds.Length >= 1)
                    {
                        string[] targetTourgroupIds = new string[rowIds.Length];
                        for (int i = 0; i < rowIds.Length; i++)
                            targetTourgroupIds[i] = mutilSelectTeamGridView.GetRowCellValue(rowIds[i], "TourgroupID").ToString();

                        msg.AppendText("\r\n开始拷贝账单数据...");
                        WcfClient.UseService<IBillCtrl>(r => r.CopyBillData(sourceTourgroupId, targetTourgroupIds, ApplicationHelper.CurrentLoginProviderNameAndID[1]));
                        msg.AppendText("\r\n账单数据拷贝完成！");
                    }
                    else
                        msg.AppendText("\r\n☆☆☆☆☆☆没有选择对应的目标团，请在目标团中打钩☆☆☆☆☆☆");

                }
                catch (Exception ex)
                {
                    msg.AppendText(ex.Message);
                }
                finally
                {
                    btnBillBatchCopy.Enabled = true;
                    Cursor.Current = Cursors.Default;
                }                 
            }           
        }
        private void btnRestaurantBatchCopy_Click(object sender, EventArgs e)
        {
            string[] copyMathTourgroupIds = null;
            string sourceTourgroupID;
            string tip = string.Format("即将复制团‘{0}’中餐厅数据到目标团中，确定吗？", singleSelectTeamGridView.GetRowCellValue(singleSelectTeamGridView.FocusedRowHandle, "TourgroupName"));
            if(MessageBox.Show(tip,"提示",MessageBoxButtons.YesNo,MessageBoxIcon.Question)== System.Windows.Forms.DialogResult.Yes)
            {
                if (CheckRestaurantDataCopyCondition(out sourceTourgroupID,out copyMathTourgroupIds) == true)
                    CopyRestaurantData(sourceTourgroupID,copyMathTourgroupIds);
            }
        }

        private void CopyRestaurantData(string sourceTourgroupId, string[] targetTourgroupIds)
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;
                btnRestaurantBatchCopy.Enabled = false;
                msg.AppendText("\r\n开始复制餐厅数据...");
                WcfClient.UseService<IRestaurantOrderCtrl>(r => r.CopyDiningAndRestaurantOrderData(sourceTourgroupId, targetTourgroupIds));
                msg.AppendText("\r\n餐厅数据复制完成！");
            }
            catch(Exception ex)
            {
                msg.AppendText(ex.Message);
            }
            finally
            {
                btnRestaurantBatchCopy.Enabled = true;
                Cursor.Current = Cursors.Default;
            } 
            
            //依据团号和本地行程计划号，拷贝ord_RestaurantOrder数据
        }
        private bool CheckRestaurantDataCopyCondition(out string sTourgroupID,out string[] copyMathTourgroupIds)
        {
            string sourceTourgroupId = sTourgroupID = singleSelectTeamGridView.GetRowCellValue(singleSelectTeamGridView.FocusedRowHandle, "TourgroupID").ToString();
            int[] rowIds = MutilRow(mutilSelectTeamGridView);
            string[] targetTourgroupIds, noMathTourgroupIds;
            bool bMatchCondition = true;
            copyMathTourgroupIds = null;

            msg.Clear();
            msg.AppendText("检测复制条件...");
            if (rowIds.Length >= 1)
            {
                targetTourgroupIds = new string[rowIds.Length];
                for (int i = 0; i < rowIds.Length; i++)
                    targetTourgroupIds[i] = mutilSelectTeamGridView.GetRowCellValue(rowIds[i], "TourgroupID").ToString();
                //检测Sch_LocalPlanSchedule表中计划行数（源、目）是否一致
                noMathTourgroupIds = WcfClient.UseService<IRestaurantOrderCtrl, string[]>(r => r.CalculateCopyRestaurantOrderDataCondition(sourceTourgroupId, targetTourgroupIds));
                
                foreach (var tourgroupId in noMathTourgroupIds)
                {
                    if (!string.IsNullOrEmpty(tourgroupId))
                    {
                        bMatchCondition = false;//不满足复制条件，不能复制
                        msg.AppendText(string.Format("\r\n计划号->{0}的团行程计划数与源不一致", tourgroupId));
                    }
                }
                this.msg.AppendText("\r\n检测完成！");
                if (bMatchCondition == true)
                    copyMathTourgroupIds = targetTourgroupIds;
            }
            else {
                bMatchCondition = false;//不满足复制条件，不能复制
                msg.AppendText("\r\n☆☆☆☆☆☆没有选择对应的目标团，请在目标团中打钩☆☆☆☆☆☆");
            }

            return bMatchCondition;
        }


        private DataTable AddGridSelectedColumn(DataTable dtSource)
        {
            dtSource.Columns.Add("Selected", typeof(bool));
            return dtSource;
        }
        private int[] MutilRow(GridView gv)
        {
            List<int> rowIds = new List<int>();
            for (int i = 0; i < gv.RowCount; i++)
            {
                object objValue = gv.GetRowCellValue(i, "Selected");
                if (objValue != null)
                {
                    bool check = false;
                    bool.TryParse(objValue.ToString(), out check);
                    if (check)
                    {
                        rowIds.Add(i);
                    }
                }
            }
            return rowIds.ToArray();
        }

        private void mutilSelectTeamGridView_Click(object sender, EventArgs e)
        {
            //if (DevGridCheckControl.ClickGridCheckBox(mutilSelectTeamGridView, "Selected", m_checkStatus))
            //{
            //    m_checkStatus = !m_checkStatus;
            //}
        }

        private void mutilSelectTeamGridView_CustomDrawColumnHeader(object sender, ColumnHeaderCustomDrawEventArgs e)
        {
            //if (e.Column != null && e.Column.FieldName == "Selected")
            //{
            //    e.Info.InnerElements.Clear();
            //    e.Painter.DrawObject(e.Info);
            //    DevGridCheckControl.DrawCheckBox(e, m_checkStatus);
            //    e.Handled = true;
            //}
        }


    }
}
